package com.yowasa.xiuxian.engine.message.battle;

import com.yowasa.xiuxian.annoations.CMD;
import com.yowasa.xiuxian.engine.message.CmdMessageHandler;
import com.yowasa.xiuxian.entity.bo.user.UserBO;
import com.yowasa.xiuxian.entity.bot.message.Message;
import com.yowasa.xiuxian.entity.enums.CmdScopeEnum;
import com.yowasa.xiuxian.entity.enums.StageEnum;
import com.yowasa.xiuxian.entity.mys.SendMessage;
import com.yowasa.xiuxian.service.helper.MessageHelper;
import com.yowasa.xiuxian.service.helper.UserHelper;
import org.springframework.beans.factory.annotation.Autowired;

@CMD(name = "/休养", scope = CmdScopeEnum.ALL)
public class RestHandler extends CmdMessageHandler {
    @Autowired
    private UserHelper userHelper;
    @Autowired
    private MessageHelper messageHelper;

    @Override
    public void execute(SendMessage message) {
        UserBO userBO = userHelper.checkOpUser(message);
        userHelper.checkCd(userBO);

        if (userBO.getUserBattle().getChp() >= userBO.getUserBattle().getHp() & userBO.getUserBattle().getCmp() >= userBO.getUserBattle().getMp()) {
            messageHelper.replyMys(message, "你当前已经是最佳状态 无需休养");
            return;
        }
        int days = revocerUser(userBO);
        userHelper.lockUser(userBO, days, userBO.getName() + "正在休养");
        userHelper.saveUserInfo(userBO);
        messageHelper.replyMys(message, "你休养了" + days + "天回复到最佳状态");
    }

    private int revocerUser(UserBO userBO) {
        int days = calRestCost(userBO);
        userBO.getUserBattle().setChp(userBO.getUserBattle().getHp());
        userBO.getUserBattle().setCmp(userBO.getUserBattle().getMp());
        return days;
    }

    private int calRestCost(UserBO userBO) {
        int day1 = (userBO.getUserBattle().getMp() - userBO.getUserBattle().getCmp())*5 / (userBO.getUserAttr().getInts() * StageEnum.getById(userBO.getStage()).getRate());
        int day2 = (userBO.getUserBattle().getHp() - userBO.getUserBattle().getChp())*5 / (userBO.getUserAttr().getCon() * StageEnum.getById(userBO.getStage()).getRate());
        int days = Math.max(day1, day2);
        if (days <= 0) {
            days = 1;
        }
        return days;
    }

    public String desc(){
        return new StringBuffer().append("耗费一定时间，将血量和法力值回复到最佳状态")
                .toString();

    }
}
